﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "BookService" in code, svc and config file together.
public class BookService : IBookService
{
    public const int SEARCH_TYPE_ALL=1;
    public const int SEARCH_TYPE_NEW = 2;
    public const int SEARCH_TYPE_HOT = 3;
    public const int SEARCH_TYPE_MYBOOK = 4;

    /// <summary>
    /// Get list category 
    /// </summary>
    /// <returns></returns>
    public ServiceListCategory GetListCategory()
    {
        var cateAdapter = new CategoryAdapter();
        var list = new ServiceListCategory {Results = cateAdapter.GetCatagoryAll()};
        return list;
    }

    /// <summary>
    /// Get book
    /// </summary>
    /// <param name="categoryId">The id category .</param>
    /// <param name="page">The page .</param>
    /// <returns></returns>
    public ServiceListBook GetBook(long categoryId, int page,int pageNumber,int typeId,string account)
    {
        BookAdapter bookAdapter = new BookAdapter();
        ServiceListBook list = new ServiceListBook();
        
        if(page == 0)
        {
            page = 1;
        }
        if (pageNumber == 0)
        {
            pageNumber = 40;
        }

        if (typeId != SEARCH_TYPE_ALL && typeId != SEARCH_TYPE_HOT && typeId != SEARCH_TYPE_MYBOOK && typeId != SEARCH_TYPE_NEW)
        {
            throw new NotImplementedException("wrong type id.");
        }
        UserAdapter userAdapter = new UserAdapter();
        if (!userAdapter.IsUserExist(account))
        {
            throw new NotImplementedException("This account do not exist.");
        }
        list.Results = bookAdapter.GetBook(categoryId, page,pageNumber,typeId,account);
        return list;
    }
    /// <summary>
    /// Search book
    /// </summary>
    /// <param name="keyWord">The keyword .</param>
    /// <param name="page">The page .</param>
    /// <returns></returns>
    public ServiceListBook SearchBook(string keyWord, int page,int pageNumber)
    {
        BookAdapter bookAdapter = new BookAdapter();
        ServiceListBook list = new ServiceListBook();

        if (page == 0)
        {
            page = 1;
        }
        if (pageNumber == 0)
        {
            pageNumber = 40;
        }

        List<BookEntity2> books = new List<BookEntity2>();
        books = bookAdapter.SearchBook(keyWord, page, pageNumber);

        list.Results = books;
        list.resultCount = books.Count;
        return list;
    }
    /// <summary>
    /// Download Book
    /// </summary>
    /// <param name="session">The session .</param>
    /// <param name="bookId">The id book .</param>
    /// <returns></returns>
    public string DownloadBook(string session, long bookId)
    {
        var userAdapter=new UserAdapter();
        if(!userAdapter.IsTokenExist(session))
        {
            throw new NotImplementedException("This account do not exist.");
        }
        else
        {
            var bookAdapter = new BookAdapter();
            return bookAdapter.GetLinkBook(bookId);
        }
    }
    /// <summary>
    /// Rate book
    /// </summary>
    /// <param name="rate">The rate .</param>
    /// <param name="bookId">The id book .</param>
    /// <returns></returns>
    public bool RateBook(int rate,long bookId)
    {
        var bookAdapter = new BookAdapter();
        return bookAdapter.RateBook(rate, bookId);
    }

    public Boolean checkSessionkey()
    {
        
    }
}
